package com.stephen.jsbridge;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import com.stephen.jsbridge.jsbridge.CallBackFunction;
import com.stephen.jsbridge.utils.DisposeWebViewJsCommand;
import com.stephen.jsbridge.utils.OnMyWebViewListener;
import com.stephen.jsbridge.utils.StephenCommonNoDataView;
import com.stephen.jsbridge.utils.StephenCrossWalkTool;

import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private StephenCrossWalkTool stephenCrossWalkTool = null;
    private StephenCommonNoDataView stephenCommonNoDataView;
    private boolean isShow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frameLayout = new FrameLayout(this);

        stephenCrossWalkTool = new StephenCrossWalkTool(this);
        stephenCommonNoDataView = new StephenCommonNoDataView(this);
        stephenCommonNoDataView.initAndInjectNoDataViewForAllView(stephenCrossWalkTool.getBridgeWebView());
        stephenCommonNoDataView.setCenterTextTopHintImg(getResources().getDrawable(R.drawable.pic_no_data_hint),100,100,5);
        stephenCommonNoDataView.setNoDataViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(stephenCommonNoDataView.isResponseClick())finish();
            }
        });
        frameLayout.addView(stephenCommonNoDataView.getFinalCreateView());

        Button button = new Button(this);
        button.setText("原生调Js方法切换内容列表展示");
        button.setOnClickListener(this);
        frameLayout.addView(button,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));

        setContentView(frameLayout);

        XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
        stephenCrossWalkTool.initJsBridgeWebView(false,new OnMyWebViewListener(this,true,true,stephenCommonNoDataView) {
            @Override
            public boolean onOverrideUrlLoading(XWalkView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(XWalkView view, String url) {
                super.onPageFinished(view,url);
                System.out.println("==================>页面加载完成");
            }
        });
        stephenCrossWalkTool.addHtmlCallNativeCommandAry(new DisposeWebViewJsCommand(this),DisposeWebViewJsCommand.ExecActionHtml5AllCommandAry);
        if(null != stephenCrossWalkTool){
            stephenCrossWalkTool.setWebHtmlLoadMode(false);
            stephenCrossWalkTool.loadUrl("http://www.5itbzm.com/#/index");
        }//end of if
    }

    @Override
    public void onClick(View v) {
        isShow = !isShow;
        String paramLoadData = "{\"isShowParam\":"+(isShow ? "\"YES\"" : "\"NO\"")+"}";
        stephenCrossWalkTool.nativeCallHtmlCommand(DisposeWebViewJsCommand.N_GoPreResultHandler, paramLoadData, new CallBackFunction() {//原生调用H5里面的东西,并传参
            @Override
            public void onCallBack(String data) {}
        });
    }
}
